package cc.bukkitPlugin.banitem.command.analyse;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.regex.Pattern;

import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import cc.bukkitPlugin.banitem.BanItem;
import cc.bukkitPlugin.banitem.CFG;
import cc.bukkitPlugin.banitem.command.AParamCommand;

public class ParamItem extends AParam{

    public ParamItem(BanItem pPlugin,AnalyseMan pAnalyor){
        super(pPlugin,pAnalyor);
        this.mLabels=new String[]{"i","item"};
    }

    @Override
    public AnalyseResult handle(CommandSender pSender,AnalyseResult pCmdParam,String pArg){
        if(pCmdParam.mNBTLabel!=null)
            return pCmdParam.buildError("参数["+pArg+"]必须在参数[nbt]前设置");
        if(Pattern.matches("\\d+",pArg)){
            try{
                int id=Integer.parseInt(pArg);
                pCmdParam.mItem=Material.getMaterial(id);
                if(pCmdParam.mItem==null||pCmdParam.mItem==Material.AIR)
                    return pCmdParam.buildError("id为["+pArg+"]的物品不存在");
            }catch(NumberFormatException nfexp){
                return pCmdParam.buildError("不是正确的数字格式");
            }
        }else{
            pArg=pArg.toUpperCase();
            pCmdParam.mItem=Material.getMaterial(pArg);
            if(pCmdParam.mItem==null||pCmdParam.mItem==Material.AIR)
                return pCmdParam.buildError("名字为["+pArg+"]的物品不存在");
        }
        return pCmdParam.buildSuccess();
    }

    @Override
    public ArrayList<String> getHelp(){
        ArrayList<String> help=new ArrayList<>();
        help.add(CFG.cmd_color+"  "+Arrays.toString(this.mLabels));
        help.add(CFG.help_color+"    设置配置的物品类型");
        help.add(CFG.help_color+"    输入可以为数字,也可以是物品英文ID");
        return help;
    }

    @Override
    public ArrayList<String> tabComplete(CommandSender pSender,AParamCommand pCommand,String[] pArgs){
        ArrayList<String> list=new ArrayList<>();
        if(pArgs==null||pArgs.length==0)
            return list;
        String[] params=pArgs[pArgs.length-1].split(":",2);
        String tLabel=params[0]+":";
        ItemStack itemInHand=null;
        if(pSender instanceof Player){
            itemInHand=((Player)pSender).getItemInHand();
            if(itemInHand==null||itemInHand.getType()==Material.AIR)
                itemInHand=null;
        }
        if(params.length==2&&!params[1].isEmpty()){
            String part=params[1].toUpperCase();
            if(part.matches("\\d+")&&!part.matches("0+\\d+")){
                if(itemInHand!=null&&(itemInHand.getTypeId()+"").startsWith(part)){
                    list.add(tLabel+itemInHand.getTypeId());
                }else{
                    for(Material tMaterial : Material.values()){
                        if((tMaterial.getId()+"").startsWith(part)){
                            list.add(tLabel+tMaterial.getId());
                            if(list.size()>=100)
                                return list;
                        }
                    }
                }
            }
            if(part.matches("[A-Z_]+")){
                if(itemInHand!=null&&itemInHand.getType().name().startsWith(part)){
                    list.add(tLabel+itemInHand.getType());
                }else{
                    for(Material tMaterial : Material.values()){
                        if(tMaterial.name().startsWith(part)){
                            list.add(tLabel+tMaterial.name());
                            if(list.size()>=100)
                                return list;
                        }
                    }
                }
            }
        }else{
            if(itemInHand!=null){
                list.add(tLabel+itemInHand.getType().name());
                list.add(tLabel+itemInHand.getTypeId()+"");
            }else pSender.sendMessage("请至少拿个东西在手上或者写点提示");
        }
        return list;
    }

}
